package com.shop.cereshop.agent.service.voucher.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.cereshop.agent.dao.voucher.AgentVoucherTransactionDAO;
import com.shop.cereshop.agent.page.voucher.AgentVoucherTransactionVO;
import com.shop.cereshop.agent.page.voucher.VoucherConsumeTotalVO;
import com.shop.cereshop.agent.param.voucher.VoucherDTO;
import com.shop.cereshop.agent.service.voucher.AgentVoucherTransactionService;
import com.shop.cereshop.commons.domain.agent.CereAgentVoucherTransaction;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.domain.wallet.CereAgentWalletTransaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AgentVoucherTransactionServiceImpl extends ServiceImpl<AgentVoucherTransactionDAO, CereAgentVoucherTransaction> implements AgentVoucherTransactionService {
    @Autowired
    private AgentVoucherTransactionDAO agentVoucherTransactionDAO;

    @Override
    public Page<AgentVoucherTransactionVO> getAgentVoucherTransaction(VoucherDTO voucherDTO) {
        PageHelper.startPage(voucherDTO.getPage(), voucherDTO.getPageSize());
        List<CereAgentVoucherTransaction> list = agentVoucherTransactionDAO.getList(voucherDTO.getAgentVoucherId());
            List<AgentVoucherTransactionVO> agentVoucherTransactionVOS=new ArrayList<>();
            for (CereAgentVoucherTransaction cereAgentVoucherTransaction : list){
                AgentVoucherTransactionVO agentVoucherTransactionVO=new AgentVoucherTransactionVO();
                agentVoucherTransactionVO.setAgentVoucherTransactionId(cereAgentVoucherTransaction.getVoucherTransactionId());
                agentVoucherTransactionVO.setTransactionMoney(cereAgentVoucherTransaction.getBalanceBefore().subtract(cereAgentVoucherTransaction.getBalanceAfter()));
                agentVoucherTransactionVO.setTransactionTime(cereAgentVoucherTransaction.getCreateTime());
                agentVoucherTransactionVO.setDescription(cereAgentVoucherTransaction.getDescription());
            }
        PageInfo<AgentVoucherTransactionVO> agentVoucherTransactionPageInfo = new PageInfo<>(agentVoucherTransactionVOS);
        return new Page<>(agentVoucherTransactionPageInfo.getList(), agentVoucherTransactionPageInfo.getTotal());
    }

    @Override
    public VoucherConsumeTotalVO getConsumptionTotal(VoucherDTO voucherDTO) {
        VoucherConsumeTotalVO consumptionTotal = agentVoucherTransactionDAO.getConsumptionTotal(voucherDTO.getAgentVoucherId());
        VoucherConsumeTotalVO voucherConsumeTotalVO = new VoucherConsumeTotalVO();
        voucherConsumeTotalVO.setBalance(consumptionTotal.getBalance());
        voucherConsumeTotalVO.setFaceValue(consumptionTotal.getFaceValue());
        voucherConsumeTotalVO.setTotalConsumeMoney(voucherConsumeTotalVO.getFaceValue().subtract(consumptionTotal.getBalance()));
        return voucherConsumeTotalVO;
    }
}
